<template>
    <div class="purchase">
        <h3 style="text-align: center;font-size: large;margin-bottom: 20px;">销售订单添加</h3>
        <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
            <Form-item label="货物编号" prop="goods_num">
                <Input class="input" icon="ios-grid" v-model="formValidate.goods_num" 
                placeholder="请输入货物编号" number></Input>
            </Form-item>

            <Form-item label="出货数量" prop="shipment_quantity">
                <Input class="input" icon="ios-calculator" v-model="formValidate.shipment_quantity"
                    placeholder="请输入出货数量" number></Input>
            </Form-item>

            <Form-item label="出货价格" prop="shipment_price">
                <Input class="input" icon="logo-yen" v-model="formValidate.shipment_price"
                    placeholder="请输采出货价格"></Input>
            </Form-item>
            <Form-item label="出货店商" prop="shipment_shops">
                <Input class="input" icon="ios-home" v-model="formValidate.shipment_shops"
                    placeholder="请输入出货店商"></Input>
            </Form-item>
            <Form-item label="负责人" prop="shipment_responsible">
                <Input class="input" icon="md-person" v-model="formValidate.shipment_responsible"
                    placeholder="请输入出货负责人"></Input>
            </Form-item>
            <Form-item>
                <i-col span="12">
                    <Button type="primary" @click="handleSubmit('formValidate',formValidate.goods_num,formValidate.shipment_quantity,formValidate.shipment_price,formValidate.shipment_shops,formValidate.shipment_responsible)">提交订单</Button>
                </i-col>
                <i-col span="12">
                    <Button type="success" @click="handleReset('formValidate')" style="margin-left: 8px">重置订单</Button>
                </i-col>
            </Form-item>
        </Form>
    </div>
</template>

<script>
    import api from '../network/api/inventory.js'
    export default {
        name: 'sales_add',

        data() {
            const validateNumber = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('选项不能为空或0'));
                }
                // 模拟异步验证效果
                setTimeout(() => {
                    if (!Number.isInteger(value)) {
                        callback(new Error('请输入数字值'));
                    } else {
                        if (value < 1) {
                            callback(new Error('不能小于1'));
                        } else {
                            callback();
                        }
                    }
                }, 1000);
            };
            return {
                formValidate: {
                    goods_num: '',
                    shipment_quantity: '',
                    shipment_price: '',
                    shipment_shops: '',
                    shipment_responsible: ''

                },
                ruleValidate: {
                    goods_num: [
                        { validator: validateNumber,required: true,  trigger: 'blur' }
                    ],
                    shipment_quantity: [
                        {validator: validateNumber, required: true, trigger: 'blur' }
                    ],
                    shipment_price: [
                        { required: true, message: '请输入出货价格', trigger: 'blur' }
                    ],
                    shipment_shops: [
                        { required: true, message: '请输入出货店商', trigger: 'blur' }
                    ],
                    shipment_responsible: [
                        { required: true, message: '请输入负责人名字', trigger: 'blur' }
                    ]
                },
            };
        },

        mounted() {

        },

        methods: {
            handleSubmit(name,goods_num,shipment_quantity,shipment_price,shipment_shops,shipment_responsible) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        let params={
                            goods_num:goods_num,
                            shipment_price:shipment_price,
                            shipment_quantity:shipment_quantity,
                            shipment_responsible:shipment_responsible,
                            shipment_shops:shipment_shops
                        }
                        api.shipment_add(params);
                        this.$Message.success('提交成功!');
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                })
            },
            handleReset(name) {
                this.$refs[name].resetFields();
            }
        },
    };
</script>

<style lang="scss" scoped>

</style>